package handler

import (
	"github.com/gin-gonic/gin"
	"pic-text-mgr/models"
	"strconv"
)

func List(c *gin.Context) {
	var pic []models.Pic

	page, _ := strconv.Atoi(c.Query("page"))
	if page <= 0 {
		page = 1
	}

	pageSize, _ := strconv.Atoi(c.Query("page_size"))
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	offset := (page - 1) * pageSize

	models.DB.Offset(offset).Limit(pageSize).Find(&pic)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": pic,
	})
	return
}
